package nl.haemers.utils 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import nl.haemers.base.IDestructible;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class Destroyer
	{
		
		public function Destroyer() 
		{
			
		}
		
		public static function destroy(container:DisplayObjectContainer) : void
		{
			if (container != null)
			{
				var child:DisplayObject;
				
				for (var i:int = container.numChildren; i > 0; i--)
				{
					child = container.getChildAt(i - 1);
					
					if (child != null)
					{
						var childContainer:DisplayObjectContainer = child as DisplayObjectContainer;
						if (childContainer != null)
						{
							destroy(childContainer);
							childContainer = null;
						}
						var destructible:IDestructible = child as IDestructible;
						if (destructible != null)
						{
							destructible.removeAllEventListeners();
							destructible = null;
						}
						container.removeChild(child);
						child = null;
					}
				}
			}
		}
		
	}

}